package com.example.costumview.customview2;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class FlowLayout2 extends ViewGroup {


    public FlowLayout2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int measureWidthSize = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeightSize = MeasureSpec.getSize(heightMeasureSpec);
        int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);

        int childCount = getChildCount();

        int lineWidth=0;//本行的宽度
        int lineHeight=0;//本行的高度

        int allWidth=0;//所有的宽度
        int allHeight=0;//所有的高度

        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);

            measureChild(childView,widthMeasureSpec,heightMeasureSpec);

            if (childView.getLayoutParams() instanceof MarginLayoutParams){

                MarginLayoutParams lp= (MarginLayoutParams) childView.getLayoutParams();

                int childWidth=childView.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
                int childHeight=childView.getMeasuredHeight()+lp.topMargin+lp.bottomMargin;

                if (lineWidth+childWidth>measureWidthSize){
                    //需要折行
                    lineWidth=childWidth;
                    lineHeight=childHeight;

                    allWidth=Math.max(lineWidth,childWidth);
                    allHeight+=childHeight;
                }else{
                    //不需要折行
                    lineWidth+=childWidth;
                    lineHeight=Math.max(lineHeight,childHeight);

                }
                if (i==childCount-1){
                    allWidth=Math.max(lineWidth,allWidth);
                    allHeight+=lineHeight;
                }
            }
        }
        setMeasuredDimension((measureWidthMode==MeasureSpec.EXACTLY)?measureWidthSize:allWidth,(measureHeightMode==MeasureSpec.EXACTLY)?measureHeightSize:allHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();

        int lineWidth=0;
        int lineHeight=0;
        int top=0,left=0;
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);

            if (childView.getLayoutParams() instanceof MarginLayoutParams){
                MarginLayoutParams lp= (MarginLayoutParams) childView.getLayoutParams();
                int childWidth=childView.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
                int childHeight=childView.getMeasuredHeight()+lp.topMargin+lp.bottomMargin;

                if (lineWidth+childWidth>getMeasuredWidth()){
                    //换行

                    lineWidth=childWidth;
                    left=0;
                    top+=lineHeight;
                    lineHeight=childHeight;
                }else{
                    //不换行
                    lineWidth+=childWidth;
                    lineHeight=Math.max(lineHeight,childHeight);
                }
                int childLeft=left+lp.leftMargin;
                int childTop=top+lp.topMargin;
                int childRight=childLeft+childView.getMeasuredWidth();
                int childBottom=childTop+childView.getMeasuredHeight();

                childView.layout(childLeft,childTop,childRight,childBottom);
                left+=childWidth;
            }

        }
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(),attrs);
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(MarginLayoutParams.MATCH_PARENT,MarginLayoutParams.MATCH_PARENT);
    }
}
